//
//  RealReachabilityListener.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/6.
//  监听网络状态，实时刷新

import UIKit
import Reachability
class ReachabilityHelper: NSObject {
    var networkStatusView = NetworkStatusView()
    var reachability: Reachability!
    @objc dynamic var isOnlineFlag: Bool = true
    static let shared = ReachabilityHelper()
    //
    func startListener(){
        do {
            reachability = try? Reachability()
            try reachability.startNotifier()
            //有网络
            reachability.whenReachable = { reachability in
                self.refreshNetworkLabel(true)
            }
            //无网络
            reachability.whenUnreachable = { _ in
                self.refreshNetworkLabel(false)
            }
        }catch {
            print("Unable to start notifier")
        }
    }
    func refreshStatus(){
        guard let _ = self.reachability else {return}
        self.refreshNetworkLabel(self.reachability.connection == .unavailable ? false : true)

    }
    //MARK: - 显示或隐藏网络状态
    private func refreshNetworkLabel(_ isConnected: Bool){
        self.isOnlineFlag = isConnected
        guard let window = Tool.getKeyWindow() else {
            return
        }
        let networkStatusViewHeight: CGFloat =  15 + (UIDevice.getBottomAreaHeight() > 0 ? 15 : 0)
        if self.networkStatusView.superview == nil{
            networkStatusView.frame = CGRect(x: 0, y: APPConfig.HeightForPortrait, width: APPConfig.WidthForPortrait, height: networkStatusViewHeight)
            window.addSubview(self.networkStatusView)//类似浮动按钮
        }
        
        window.bringSubviewToFront(self.networkStatusView)
        if isConnected {//有网络： 网络重连后，判断版本是否更新
            VersionManager.shared.checkForceUpdateVersion() 
            self.networkStatusView.setDataInfo(true)
            UIView.animate(withDuration: 0.5, delay: 1, options: UIView.AnimationOptions.allowAnimatedContent, animations: {
                self.networkStatusView.frame = CGRect(x: 0, y: APPConfig.HeightForPortrait , width: APPConfig.WidthForPortrait, height: networkStatusViewHeight)
            }) { (_) in
                self.networkStatusView.isHidden = true
            }
        }else{//无网络
            networkStatusView.frame = CGRect(x: 0, y: APPConfig.HeightForPortrait - networkStatusViewHeight, width: APPConfig.WidthForPortrait, height: networkStatusViewHeight)
            self.networkStatusView.setDataInfo(false)
            UIView.animate(withDuration: 0.5) {
                self.networkStatusView.isHidden = false
            }
        }
    }
    //MARK: - 将网络提示显示在最前边
    func showNetworkStatusView(){
        if let _ = self.networkStatusView.superview,let window = Tool.getKeyWindow(){
            window.bringSubviewToFront(self.networkStatusView)
        }
    }
}
class NetworkStatusView: View {
    lazy var label: ContentLabel = {
        let view = ContentLabel(R.font.interMedium(size: 9), hexStr: "#FFFFFF")
        view.textAlignment = .center
        return view
    }()
    override func setUpView() {
        //
        label.frame = CGRect(x: 0, y: 1, width: APPConfig.WidthForPortrait, height: 13)
        self.addSubview(label)
    }
    func setDataInfo(_ reachable: Bool){
        if reachable {
            label.text = R.string.localizable.backonlinE()
            self.backgroundColor = UIColor(hexStr: Constant.ThemeColorHexStr)
        }else{
            label.text = R.string.localizable.nointernetconnectioN()
            self.backgroundColor = UIColor.red
        }
    }
}
